<?php

class ThongKeChiController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }

        if ($action == 'thongketuan') {

            $BEP_ID = $_POST['BEP_ID'];
            $THANG = $_POST['THANG'];
            $TUAN = $_POST['TUAN'];
            $results = $this->showdulieu($BEP_ID, $THANG, $TUAN);

            $beps = $this->getBepId($BEP_ID);


            $dulieu = '';
            if ($results->getNHAP_TONG_TIEN_PHAI_TRA() != null) {


                $dulieu = ' <thead>
       <tr  class="ui-bar-d">
        <th data-priority="5">Bếp</th>
                          <th data-priority="3">Tuần</th>
                          <th data-priority="5">Tháng</th>
                           <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead>
     <tbody>
       <tr>
        <td>' . $beps->getBEP_TEN_BEP() . '</td>
         <td>' . $TUAN . '</td>
         <td>' . $THANG . '</td>
        <td>' . $results->getMAX_NHAP_NGAY_NHAP() . ' - ' . $results->getMIN_NHAP_NGAY_NHAP() . '</td>     
         <td>' . Utility::formatToCurrencyString($results->getNHAP_TONG_TIEN_PHAI_TRA()) . ' (VNĐ)</td>
       </tr>
       
     </tbody>';
            } else {
                $dulieu = '<thead>
       <tr  class="ui-bar-d">
         <th data-priority="5">Bếp</th>
                          <th data-priority="3">Tuần</th>
                          <th data-priority="5">Tháng</th>
                           <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead> ';
            }
            echo $dulieu;
            exit();
        } else if ($action == 'thongkethang') {

            $BEP_ID = $_POST['BEP_ID'];
            $THANG = $_POST['THANG'];
            $results = $this->showdulieu_thang($BEP_ID, $THANG);

            $beps = $this->getBepId($BEP_ID);


            $dulieu = '';
            if ($results->getNHAP_TONG_TIEN_PHAI_TRA() != null) {


                $dulieu = ' <thead>
       <tr  class="ui-bar-d">
         <th data-priority="5">Bếp</th>
                          <th data-priority="5">Tháng</th>
                          <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead>
     <tbody>
       <tr>
        <td>' . $beps->getBEP_TEN_BEP() . '</td>
         <td>' . $THANG . '</td>
         <td>' . $results->getMAX_NHAP_NGAY_NHAP() . ' - ' . $results->getMIN_NHAP_NGAY_NHAP() . '</td>
         <td>' . Utility::formatToCurrencyString($results->getNHAP_TONG_TIEN_PHAI_TRA()) . ' (VNĐ)</td>
       </tr>
       
     </tbody>';
            } else {
                $dulieu = '<thead>
       <tr  class="ui-bar-d">
         <th>Bếp</th>
         <th data-priority="5">Tháng</th>
          <th data-priority="5">Từ ngày đến ngày</th>
         <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead> ';
            }
            echo $dulieu;
            exit();
        }else if ($action == 'thongke_tungay_denngay') {

            $BEP_ID = $_POST['BEP_ID'];
            $TUNGAY = $_POST['TUNGAY'];
            $DENNGAY = $_POST['DENNGAY'];
            $results = $this->showdulieu_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY);

            $beps = $this->getBepId($results->getBEP_ID());


            $dulieu = '';
            if ($results->getNHAP_TONG_TIEN_PHAI_TRA() != null) {


                $dulieu = ' <thead>
       <tr  class="ui-bar-d">
         <th data-priority="5">Bếp</th>
                          <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead>
     <tbody>
       <tr>
        <td>' . $beps->getBEP_TEN_BEP() . '</td>
         <td>' .$TUNGAY. ' - ' . $DENNGAY. '</td>
         <td>' . Utility::formatToCurrencyString($results->getNHAP_TONG_TIEN_PHAI_TRA()) . ' (VNĐ)</td>
       </tr>
     </tbody>';
            } else {
                $dulieu = '<thead>
       <tr  class="ui-bar-d">
         <th>Bếp</th>
          <th data-priority="5">Từ ngày đến ngày</th>
         <th data-priority="1">Tổng tiền</th>
       </tr>
     </thead> ';
            }
            echo $dulieu;
            exit();
        }

        // nếu task là edit thì include trang thêm vào
        if ($task == '') {
            $viewFile = $dir . '/views/thongkechi.php';
            $xtemplate = new XTemplate($viewFile);

            $idbep = '';
            $tenbep = '';
            $thang = 0;
            $tuan = 0;


            $maxthang = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang_');
            }

            $maxthang_ = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang_->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang');
            }
            
            
            $maxtuan_thang = $this->getMaxTuan($maxthang->getTHANG_ID());
            for ($i = 1; $i <= $maxtuan_thang->getTUAN_ID(); $i++) {
                if ($maxtuan_thang->getTUAN_ID() == $i) {
                    $xtemplate->assign('TUAN', '<option selected="selected" value="' . $i . '">Tuần ' . $i . '</option>');
                    $tuan = $i;
                } else {
                    $xtemplate->assign('TUAN', '<option value="' . $i . '">Tuần ' . $i . '</option>');
                }
                $xtemplate->parse('main.tuan');
            }

            $beps = $this->getAllBep();
            $index = 1;
            if ($beps != null) {

                foreach ($beps as $bep) {

                    if ($index == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    $index++;
                }
            }

            $beps_ = $this->getAllBep();
            $index_ = 1;
            if ($beps_ != null) {

                foreach ($beps_ as $bep) {

                    if ($index_ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep_');
                    $index_++;
                }
            }
            
            $beps__ = $this->getAllBep();
            $index__ = 1;
            if ($beps__ != null) {

                foreach ($beps__ as $bep) {

                    if ($index__ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep__');
                    $index__++;
                }
            }
            
            
            $results = $this->showdulieu($idbep, $thang, $tuan);
            if ($results->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                $xtemplate->assign('TUAN', $tuan);
                $xtemplate->assign('TUNGAY', $results->getMIN_NHAP_NGAY_NHAP());
                $xtemplate->assign('DENNGAY', $results->getMAX_NHAP_NGAY_NHAP());
                $xtemplate->assign('TONGTIEN', Utility::formatToCurrencyString($results->getNHAP_TONG_TIEN_PHAI_TRA()));

                $xtemplate->parse('main.thongke');
            }

            $results1 = $this->showdulieu_thang($idbep, $thang);
            if ($results1->getNHAP_TONG_TIEN_PHAI_TRA() != null) {
                $xtemplate->assign('BEP_ID', $tenbep);
                $xtemplate->assign('THANG', $thang);
                 $xtemplate->assign('TUNGAY', $results1->getMIN_NHAP_NGAY_NHAP());
                $xtemplate->assign('DENNGAY', $results1->getMAX_NHAP_NGAY_NHAP());
                $xtemplate->assign('TONGTIEN', Utility::formatToCurrencyString($results1->getNHAP_TONG_TIEN_PHAI_TRA()));

                $xtemplate->parse('main.thongkethang');
            }


            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getAllThang() {
        $db = new Database();
        try {
            $nhap = new Nhap();

            $result = $nhap->getAllThang($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getMaxThang() {
        $db = new Database();
        try {
            $nhap = new Nhap();

            $result = $nhap->getMaxThang($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getMaxTuan($idthang) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $nhap->setTHANG_ID($idthang);
            $result = $nhap->getMaxTuan($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showdulieu($idbep, $thang, $tuan) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $result = $nhap->showdulieu($idbep, $thang, $tuan, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showdulieu_thang($idbep, $thang) {
     
        $db = new Database();
        try {
            $nhap = new Nhap();
            $result = $nhap->showdulieu_thang($idbep, $thang, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY) {
     
        $db = new Database();
        try {
            $nhap = new Nhap();
            $result = $nhap->showdulieu_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getBepId($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
